package com.youlu.campus.admin.jinshuju;

import com.youlu.campus.admin.annotation.RequestActivityAuth;
import com.youlu.campus.admin.utils.AuthUtils;
import com.youlu.campus.admin.utils.ExcelUtils;
import com.youlu.campus.entity.jinshuju.JinMockSignUserImport;
import com.youlu.campus.entity.jinshuju.JinUserInviteImport;
import com.youlu.campus.entity.jinshuju.JinUserParticipate;
import com.youlu.campus.entity.jinshuju.dto.JinInviteStatisticsQueryDTO;
import com.youlu.campus.entity.jinshuju.dto.JinUserParticipateQueryDTO;
import com.youlu.campus.entity.jinshuju.dto.QueryJinUserImportDTO;
import com.youlu.campus.service.jinshuju.JinInviteStatisticsService;
import com.youlu.campus.service.jinshuju.JinShuJuUserActivityService;
import com.youlu.campus.service.jinshuju.JinUserImportService;
import com.yuelin.infrastructure.quantum.common.QResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Slf4j
@RestController
public class JinUserParticipateController {

    @Autowired
    private JinShuJuUserActivityService jinShuJuUserActivityService;

    @Autowired
    private JinUserImportService jinUserImportService;

    @Autowired
    private JinInviteStatisticsService jinInviteStatisticsService;

    /**
     * 金数据活动用户报名列表接口
     *
     * @param dto
     * @return
     */
    @PostMapping("/jinshuju/activity/sign/user/list")
    public QResult list(@RequestBody JinUserParticipateQueryDTO dto) {
        return QResult.success(jinShuJuUserActivityService.list(dto));
    }

    /**
     * 更新金数据活动用户报名接口
     *
     * @param updateJinUserParticipate
     * @return
     */
    @PostMapping("/jinshuju/activity/sign/user/update")
    @RequestActivityAuth(activityId = "#updateJinUserParticipate.activityId")
    public QResult update(@RequestBody JinUserParticipate updateJinUserParticipate) {
        jinShuJuUserActivityService.updateJinUserParticipate(updateJinUserParticipate);
        return QResult.success(null);
    }

    /**
     * 金数据活动用户报名详情接口
     *
     * @param id
     * @return
     */
    @GetMapping("/jinshuju/activity/sign/user/detail")
    public QResult detail(String id) {
        return QResult.success(jinShuJuUserActivityService.detail(id));
    }

    /**
     * 金数据活动用户数据mock 志愿这证书获取
     *
     * @param activityId \@param file
     * @return
     */
    @PostMapping("/jinshuju/activity/sign/user/import")
    public QResult importInviteUser(@RequestParam("file") MultipartFile file,
                                    @RequestParam("activityId") String activityId) {
        List<JinUserInviteImport> excelDataList = ExcelUtils.readExcel(file, JinUserInviteImport.class);
        if (CollectionUtils.isEmpty(excelDataList)) {
            return QResult.fail("导入文件错误");
        }
        jinUserImportService.userImport(activityId, excelDataList);
        return QResult.success(null);
    }

    /**
     * 金数据活动用户数据mock 报名已获取
     *
     * @param activityId \@param file
     * @return
     */
    @PostMapping("/jinshuju/activity/user/feed/import")
    public QResult importSignUpUser(@RequestParam("file") MultipartFile file,
                                    @RequestParam("activityId") String activityId) {
        List<JinMockSignUserImport> excelDataList = ExcelUtils.readExcel(file, JinMockSignUserImport.class);
        if (CollectionUtils.isEmpty(excelDataList)) {
            return QResult.fail("导入文件错误");
        }
        jinUserImportService.userFeedImport(activityId, excelDataList);
        return QResult.success(null);
    }

    /**
     * 金数据活动用户报名导入list
     *
     * @param dto
     * @return
     */
    @PostMapping("/jinshuju/activity/sign/user/mock/list")
    public QResult importInviteUserMockList(@RequestBody QueryJinUserImportDTO dto) {
        return QResult.success(jinUserImportService.list(dto));
    }

    /**
     * 金数据活动用户报名导入详情
     *
     * @param id
     * @return
     */
    @GetMapping("/jinshuju/activity/sign-user/mock/detail")
    public QResult importInviteUserMockDetail(String id) {
        return QResult.success(jinUserImportService.detail(id));
    }


    /**
     * 金数据活动用户报名导入删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/jinshuju/activity/sign-user/mock/delete/{id}")
    public QResult importInviteUserMockDelete(@PathVariable String id) {
        jinUserImportService.delete(id);
        return QResult.success(null);
    }


    /**
     * 金数据活动用户报名导入批量删除
     *
     * @param activityId
     * @return
     */
    @DeleteMapping("/jinshuju/activity/sign-user/mock/delete-batch/{activityId}")
    public QResult importInviteUserMockDeleteBatch(@PathVariable String activityId) {
        jinUserImportService.batchDelete(activityId, JinUserInviteImport.class);
        return QResult.success(null);
    }

    /**
     * 导出-金数据活动用户报名列表接口
     *
     * @param dto
     * @return
     */
    @PostMapping("/jinshuju/activity/sign/user/export")
    @RequestActivityAuth(activityId = "#dto.activityId")
    public QResult export(@RequestBody JinUserParticipateQueryDTO dto) {
        dto.setOptUserName(AuthUtils.getLoginName());
        dto.setOptUserId(AuthUtils.getUserId());
        jinShuJuUserActivityService.export(dto);
        return QResult.success(null);
    }

    /**
     * 金数据邀请统计
     *
     * @param dto
     * @return
     */
    @PostMapping("/jinshuju/activity/invite/statistics")
    public QResult jinInviteStatistics(@Validated @RequestBody JinInviteStatisticsQueryDTO dto) {
        return QResult.success(jinInviteStatisticsService.queryStatisticsData(dto));
    }

    /**
     * 金数据邀请裂变率统计
     *
     * @param dto
     * @return
     */
    @PostMapping("/jinshuju/activity/invite/radio")
    public QResult jinInviteRadio(@Validated @RequestBody JinInviteStatisticsQueryDTO dto) {
        return QResult.success(jinInviteStatisticsService.queryStatisticsRadio(dto));
    }

    /**
     * 金数据活动用户报名手机号修改
     *
     * @param dto
     * @return
     */
    @PostMapping("/jinshuju/activity/sign/user/info/update-mobile")
    public QResult updateInfo(@RequestBody JinUserParticipateQueryDTO dto) {
        return QResult.success(jinShuJuUserActivityService.updateMobile(dto.getId(), dto.getMobile()));
    }

}
